template <typename U>
struct PreOrderIterator{
Node<U>* current;
explicit PreOrderIterator(Node<U>* current): current(current) {}
bool operator!=(const PreOrderIterator<U>& other){
return current!=other.current;
}
Node<U>& operator*(){ return *current; }
PreOrderIterator<U>& operator++(){
if(current->right){
current=current->right;
while(current->left) current=current->left;
} else {
Node<T>* p=current->parent;
while(p && current == p->right){
current=p;
p=p->parent;
}
current=p;
}
return *this;
}
typedef PreOrderIterator<T> iterator;
template <typename T>
iterator BinaryTree<T>::begin(){
Node<T>* n=root;
if(n)
while(n->left) n=n->left;
return iterator{n};
}
template <typename T>
iterator BinaryTree<T>::end(){
return iterator{nullptr};
}